﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Hack.Extensions;

namespace Hack
{
    public class MultiThreading
    {
        /// <summary>
        /// 2x-3x faster than normal c# loop, 
        /// ex: Synced [0-1-2-3-4-5-6]
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="action"></param>
        public void ForEach_Synced<T>(IList<T> list, Action<T> action)
        {
            int i = 0;
            Func<int, bool> LoopDone = (value) =>
            {
                return (value < list.Count);
            };

            new Thread(new ThreadStart(() =>
            {
                lock (this)
                {
                    while (true)
                    {
                        if (LoopDone(i))
                            action(list[i++]);
                        else break;
                    }
                }
            })).Start();
            new Thread(new ThreadStart(() =>
            {
                lock (this)
                {
                    while (true)
                    {
                        if (LoopDone(i))
                            action(list[i++]);
                        else break;
                    }
                }
            })).Start();
        }
        /// <summary>
        /// 2x-3x faster than normal c# loop, 
        /// ex: Non Synced [0-1-3-2-5-4-6], The CPU that handles the calculation
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="action"></param>
        public void ForEach_NonSynced<T>(IList<T> list, Action<T> action)
        {
            int i = 0;
            Func<int, bool> LoopDone = (value) =>
            {
                return (value < list.Count);
            };

            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    if (LoopDone(i))
                        action(list[i++]);
                    else break;
                }

            })).Start();
            new Thread(new ThreadStart(() =>
            {

                while (true)
                {
                    if (LoopDone(i))
                        action(list[i++]);
                    else break;
                }

            })).Start();
        }

    }
}